/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.telsoft.data;

import com.telsoft.business.DAOHelperBusiness;
import com.telsoft.entity.AttributesEntity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author thieplv
 */
public class AttributesDAO {

    private Connection conn;

    public List<AttributesEntity> getAttrs() throws SQLException {
        List<AttributesEntity> attrs = new ArrayList<AttributesEntity>();
        conn = DAOHelperBusiness.getConnection();
        Statement stmAttr = DAOHelperBusiness.getStatement(conn);
        
        String sqlAttr = "SELECT * FROM ATTRIBUTES";
        ResultSet rsAttr = stmAttr.executeQuery(sqlAttr);
        
        while (rsAttr.next()) {
            AttributesEntity attr = new AttributesEntity();     
            attr.setAttID(rsAttr.getInt("ATT_ID"));
            attr.setDescription(rsAttr.getString("DECRIPTION"));
            attr.setType(rsAttr.getBoolean("TYPE"));
            attr.setName(rsAttr.getString("NAME"));
            attrs.add(attr);
        }     
        DAOHelperBusiness.closeResultSet(rsAttr);
        DAOHelperBusiness.closeStatement(stmAttr);
        DAOHelperBusiness.closeConnection(conn);        
        return attrs;
    }

    public List<AttributesEntity> getAttrsByChildID(int id) throws SQLException {
        List<AttributesEntity> attrs = new ArrayList<AttributesEntity>();
        conn = DAOHelperBusiness.getConnection();
        String sql = "SELECT ATTRIBUTES.ATT_ID,ATTRIBUTES.NAME,ATTRIBUTES.TYPE,ATTRIBUTES.DECRIPTION"
                + " FROM  CHILD_ATT,ATTRIBUTES"
                + " WHERE CHILD_ATT.ATT_ID = ATTRIBUTES.ATT_ID"
                + " and CHILD_ATT.CHILD_ID = ?";
        PreparedStatement preStm = DAOHelperBusiness.getPreparedStatement(conn, sql);
        preStm.setInt(1, id);
        ResultSet rs = preStm.executeQuery();
        while (rs.next()) {
            AttributesEntity attr = new AttributesEntity();
            attr.setAttID(rs.getInt("ATT_ID"));
            attr.setName(rs.getString("NAME"));
            attr.setDescription(rs.getString("DECRIPTION"));
            attr.setType(rs.getBoolean("TYPE"));
            attrs.add(attr);
        }
        DAOHelperBusiness.closeResultSet(rs);
        DAOHelperBusiness.closePreparedStatement(preStm);
        DAOHelperBusiness.closeConnection(conn);
        return attrs;
    }
}
